package ihm.views.main.catalog;

import ihm.actions.FilterAction;
import ihm.actions.FilterReset;
import ihm.models.ATKmodel;

import java.text.NumberFormat;
import java.util.LinkedList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;

import systems.languages.MultiLanguages;
import systems.rpgBuilder.Faction;
import systems.rpgBuilder.RpgBuilder;
import systems.rpgBuilder.Type;
/**
 * 
 * @author Baptiste Legrand & Benjamin Orman
 * @version 2.0
 */
public class FilterBar extends JPanel{

  private static final long serialVersionUID = 1L;

  private JLabel labelFilter = new JLabel();
  private JLabel labelFaction= new JLabel();
  private JComboBox comboBoxFaction = new JComboBox();
  private JLabel labelType= new JLabel();
  private JComboBox comboBoxType  = new JComboBox();
  private JLabel labelLevel= new JLabel();
  private JLabel labelMinLevel= new JLabel();
  private JLabel labelMaxLevel= new JLabel();
  private JFormattedTextField textFieldMinLevel, textFieldMaxLevel;
  private JButton filterButton= new JButton();
  private JButton resetButton=new JButton();
  private ATKmodel model;

  public FilterBar(ATKmodel model)
  {
    super();
    updateFilterBar();
    this.model = model;
    this.createsLevelTextFields();
    this.buildFilterBar();
  }

  private void createsLevelTextFields() {
    this.textFieldMinLevel = new JFormattedTextField(NumberFormat.INTEGER_FIELD);
    this.textFieldMinLevel.setColumns(2);
    this.textFieldMinLevel.setText(String.valueOf(RpgBuilder.getMinLevel()));
    this.textFieldMaxLevel = new JFormattedTextField(NumberFormat.INTEGER_FIELD);
    this.textFieldMaxLevel.setColumns(2);
    this.textFieldMaxLevel.setText(String.valueOf(RpgBuilder.getMaxLevel()));
  }


  private void buildFactionBox()
  {
    this.comboBoxFaction.removeAllItems();
    List<Faction> factions = RpgBuilder.getFactionsList();
    if(factions.size()>0)
    {
      this.comboBoxFaction.addItem(MultiLanguages.getProperties("factions.All.label"));
      for(Faction faction : factions)
        this.comboBoxFaction.addItem(MultiLanguages.getProperties(faction.getMultiLanguageName()));
      this.comboBoxFaction.setEnabled(true);
    }else{
      this.comboBoxFaction.addItem(MultiLanguages.getProperties("factions.Empty.label"));
      this.comboBoxFaction.setEnabled(false);
    }
  }

  private void buildTypeBox()
  {
    LinkedList<Type> types;
    try{
      types = model.getSelectedItem().getItemTypesList();
    }catch(Exception e){
      types = new LinkedList<Type>();
    }  
    this.updateType(types);
  }

  public void updateFilterBar()
  {
    String boldPreff = "<html><b>";
    String boldSuff = "</b></html>";
    this.labelFilter.setText(MultiLanguages.getProperties("filter.Filter.label"));
    this.labelFaction.setText(boldPreff+MultiLanguages.getProperties("filter.Faction.label")+boldSuff);
    this.labelType.setText(boldPreff+MultiLanguages.getProperties("filter.Type.label")+boldSuff);
    this.labelLevel.setText(boldPreff+MultiLanguages.getProperties("filter.Level.label")+boldSuff);
    this.labelMinLevel.setText(MultiLanguages.getProperties("filter.MinLevel.label"));
    this.labelMaxLevel.setText(MultiLanguages.getProperties("filter.MaxLevel.label"));
    this.filterButton.setText(MultiLanguages.getProperties("filter.Filter.button"));
    this.resetButton.setText(MultiLanguages.getProperties("filter.Reset.button"));
    buildFactionBox();

    buildTypeBox();
  }

  private void buildFilterBar()
  {
    this.add(this.labelFilter);

    this.add(this.labelFaction);
    this.add(this.comboBoxFaction);
    this.add(new JLabel(" | "));
    this.add(this.labelType);
    this.add(this.comboBoxType);
    this.add(new JLabel(" | "));
    this.add(this.labelLevel);
    this.add(this.labelMinLevel);
    this.add(this.textFieldMinLevel);
    this.add(this.labelMaxLevel);
    this.add(this.textFieldMaxLevel);
    this.add(new JLabel(" | "));
    this.add(this.filterButton);
    this.filterButton.addActionListener(new FilterAction(this, this.model));
    this.add(this.resetButton);
    this.resetButton.addActionListener(new FilterReset(this, this.model));
  }

  public void updateType(LinkedList<Type> itemTypesList) {
    this.comboBoxType.removeAllItems();
 
    if(itemTypesList.size()>0)
    {
      this.comboBoxType.addItem(MultiLanguages.getProperties("type.All.label"));
      for(Type type : itemTypesList)
        this.comboBoxType.addItem(MultiLanguages.getProperties(type.getMultiLangueName()));
      this.comboBoxType.setEnabled(true);
    }else{
      this.comboBoxType.addItem(MultiLanguages.getProperties("type.Empty.label"));
      this.comboBoxType.setEnabled(false);
    }
  }

  /**
   * 
   * @return The id of faction selected.
   */
  public int getFactionSelected() {
    return comboBoxFaction.getSelectedIndex();
  }

  /**
   * @return The id of the type selected.
   */
  public int getTypeSelected() {
    return comboBoxType.getSelectedIndex();
  }

  /**
   * @return the textFieldMinLevel
   */
  public int getMinLevel() {
    int minLevel = Integer.valueOf(textFieldMinLevel.getText());
    int rpgMinLevel = RpgBuilder.getMinLevel();
    if(minLevel < rpgMinLevel)
    {
      minLevel = rpgMinLevel;
      textFieldMinLevel.setText(String.valueOf(minLevel));
    }
    return minLevel;
  }

  /**
   * @return the textFieldMaxLevel
   */
  public int getMaxLevel() {
    int maxLevel = Integer.valueOf(textFieldMaxLevel.getText());
    int rpgMaxLevel = RpgBuilder.getMaxLevel();
    if(maxLevel > rpgMaxLevel)
    {
      maxLevel = rpgMaxLevel;
      textFieldMinLevel.setText(String.valueOf(maxLevel));
    }
    return maxLevel;
  }

  /**
   * Reset all the field
   */
  public void reset() {
    if(this.comboBoxFaction.isEnabled())
      this.comboBoxFaction.setSelectedIndex(0);
    
    if(this.comboBoxType.isEnabled())
      this.comboBoxType.setSelectedIndex(0);
    
    this.textFieldMinLevel.setText(String.valueOf(RpgBuilder.getMinLevel()));
    this.textFieldMaxLevel.setText(String.valueOf(RpgBuilder.getMaxLevel()));
  }

}
